package com.thfw.test.view.activity;

import android.app.AlertDialog;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Button;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bigkoo.pickerview.TimePickerView;

import android.content.DialogInterface;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.thfw.test.model.authManage.AuthBaseBean;
import com.thfw.test.model.authManage.LampInterfaceBean;
import com.thfw.test.model.bean.BindingBean;
import com.thfw.test.model.bean.GroupBean;
import com.thfw.test.model.bean.LampListBean;
import com.thfw.test.model.bean.LightAdapter;
import com.thfw.test.model.bean.MainEnergyBean;
import com.thfw.test.model.bean.RegionBean;
import com.thfw.test.utils.ConfigurationEntry;
import com.thfw.test.utils.StringUtils;
import com.thfw.test.view.network.AuthManageService;
import com.thfw.wmd.R;
import com.thfw.test.utils.ToastUtil;
import com.thfw.test.utils.ToastUtils;
import com.thfw.test.view.fragment.AboutMeFragment;
import com.thfw.test.view.fragment.DeviceManagementFragment;
import com.thfw.test.view.fragment.EnergyManagementFragment;
import com.thfw.test.view.fragment.LightControlFragment;
import com.thfw.test.view.widget.LineChart02View;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import scooper.cn.sc_base.log.SCLog;
import utils.AppActivities;
import utils.RetrofitWrapper;
import utils.ViewSelectGroup;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xclcharts.chart.LineData;
import org.xclcharts.renderer.XEnum;

import java.net.MalformedURLException;
import java.security.acl.Group;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import android.graphics.Color;
import android.widget.Toast;
import android.os.Message;
import android.os.Handler;
import android.text.format.DateFormat;

import com.thfw.test.view.widget.StudyBendLine;
import com.thfw.test.view.widget.TouchProgessView;
import com.thfw.test.view.widget.AnalogController;

import static com.thfw.test.model.bean.RegionBean.prinfAllRegion;

/**
 * Created BY Cuishikuntv_label
 * Created ON 2021/11/10
 * Package Name com.thfw.test.view.activity
 **/
public class ControlActivity extends BaseActivity {
    public static int themeColor = Color.parseColor("#30CA6F");
    private static final String TAG = ControlActivity.class.getCanonicalName();
    @BindView(R.id.rl_group_all)
    RelativeLayout rl_group_all;
    @BindView(R.id.rl_group1)
    RelativeLayout rl_group1;
    @BindView(R.id.rl_group2)
    RelativeLayout rl_group2;
    @BindView(R.id.rl_group3)
    RelativeLayout rl_group3;
    @BindView(R.id.rl_group_radom)
    RelativeLayout rl_group_radom;
    @BindView(R.id.tv_light1)
    TextView tv_light1;
    @BindView(R.id.tv_light2)
    TextView tv_light2;
    @BindView(R.id.tv_light3)
    TextView tv_light3;
    @BindView(R.id.tv_light4)
    TextView tv_light4;
    @BindView(R.id.tv_light5)
    TextView tv_light5;
    @BindView(R.id.tv_light6)
    TextView tv_light6;
    @BindView(R.id.tv_light7)
    TextView tv_light7;
    @BindView(R.id.tv_light8)
    TextView tv_light8;
    @BindView(R.id.tv_light9)
    TextView tv_light9;
    @BindView(R.id.iv_light1)
    ImageView iv_light1;
    @BindView(R.id.iv_light2)
    ImageView iv_light2;
    @BindView(R.id.iv_light3)
    ImageView iv_light3;
    @BindView(R.id.iv_light4)
    ImageView iv_light4;
    @BindView(R.id.iv_light5)
    ImageView iv_light5;
    @BindView(R.id.iv_light6)
    ImageView iv_light6;
    @BindView(R.id.iv_light7)
    ImageView iv_light7;
    @BindView(R.id.iv_light8)
    ImageView iv_light8;
    @BindView(R.id.iv_light9)
    ImageView iv_light9;
    @BindView(R.id.tv_group_all)
    TextView tv_group_all;
    @BindView(R.id.tv_group1)
    TextView tv_group1;
    @BindView(R.id.tv_group2)
    TextView tv_group2;
    @BindView(R.id.tv_group_radom)
    TextView tv_group_radom;
    @BindView(R.id.rl_aab)
    RelativeLayout rl_aab;
    @BindView(R.id.rl_aabb)
    RelativeLayout rl_aabb;
    @BindView(R.id.tv_group3)
    TextView tv_group3;
    @BindView(R.id.linechart1)
    StudyBendLine view1;
    @BindView(R.id.view_pg)
    TouchProgessView view_pg;
    @BindView(R.id.anlog1)
    AnalogController bassController;
    @BindView(R.id.anlog2)
    AnalogController bassController2;
    @BindView(R.id.iv_group_right)
    ImageView iv_group_right;
    @BindView(R.id.iv_group_left)
    ImageView iv_group_left;
    @BindView(R.id.iv_light_open)
    ImageView iv_light_open;
    @BindView(R.id.iv_jbsj_add)
    ImageView iv_jbsj_add;
    @BindView(R.id.iv_jbsj_delete)
    ImageView iv_jbsj_delete;
    @BindView(R.id.iv_cxsj_add)
    ImageView iv_cxsj_add;
    @BindView(R.id.iv_cxsj_delete)
    ImageView iv_cxsj_delete;
    @BindView(R.id.tv_jbsj_time)
    TextView tv_jbsj_time;
    @BindView(R.id.tv_cxsj_time)
    TextView tv_cxsj_time;
    @BindView(R.id.tv_kdsj_time_hour)
    TextView tv_kdsj_time_hour;
    @BindView(R.id.tv_kdsj_time_min)
    TextView tv_kdsj_time_min;
    @BindView(R.id.tv_gdsj_time_hour)
    TextView tv_gdsj_time_hour;
    @BindView(R.id.tv_gdsj_time_min)
    TextView tv_gdsj_time_min;
    @BindView(R.id.tv_yxsj)
    TextView tv_yxsj;

    @BindView(R.id.tv_jrnh_num)
    TextView tv_jrnh_num;
    @BindView(R.id.tv_jrnh_num2)
    TextView tv_jrnh_num2;
    @BindView(R.id.tv_jrnh_num3)
    TextView tv_jrnh_num3;


    @BindView(R.id.iv_kdsj_add)
    ImageView iv_kdsj_add;
    @BindView(R.id.iv_gdsj_add)
    ImageView iv_gdsj_add;
    @BindView(R.id.tv_control_time)
    TextView tv_control_time;
    @BindView(R.id.iv_control_power_off)
    ImageView iv_control_power_off;
    @BindView(R.id.tv_lable_all)
    TextView tv_lable_all;
    @BindView(R.id.cb_lighr1)
    CheckBox cb_lighr1;
    @BindView(R.id.cb_lighr2)
    CheckBox cb_lighr2;
    @BindView(R.id.cb_lighr3)
    CheckBox cb_lighr3;
    @BindView(R.id.cb_lighr4)
    CheckBox cb_lighr4;
    @BindView(R.id.cb_lighr5)
    CheckBox cb_lighr5;
    @BindView(R.id.cb_lighr6)
    CheckBox cb_lighr6;
    @BindView(R.id.cb_lighr7)
    CheckBox cb_lighr7;
    @BindView(R.id.cb_lighr8)
    CheckBox cb_lighr8;
    @BindView(R.id.cb_lighr9)
    CheckBox cb_lighr9;
    boolean isOpenGroupAll;
    boolean isOpenGroup1;
    boolean isOpenGroup2;
    boolean isOpenGroup3;
    boolean isOpenGroupRandom;

    boolean isOpenLight1;
    boolean isOpenLight2;
    boolean isOpenLight3;
    boolean isOpenLight4;
    boolean isOpenLight5;
    boolean isOpenLight6;
    boolean isOpenLight7;
    boolean isOpenLight8;
    boolean isOpenLight9;

    int jbsjGroup1 = 5;
    int jbsjGroup2 = 5;
    int jbsjGroup3 = 5;
    int jbsjGroup4 = 5;
    int jbsjGroupRadom = 5;

    int cxsjGroup1 = 15;
    int cxsjGroup2 = 15;
    int cxsjGroup3 = 15;
    int cxsjGroup4 = 15;
    int cxsjGroupRadom = 15;

    int group1Down = 1;
    int group2Down = 1;
    int group3Down = 1;
    int group4Down = 1;
    int groupRadomDown = 1;

    int group1Up = 1;
    int group2Up = 1;
    int group3Up = 1;
    int group4Up = 1;
    int groupRadomUp = 1;

    int group1Progress;
    int group2Progress;
    int group3Progress;
    int group4Progress;
    int group5Progress;

    String group1TimeOpenHour = "00";
    String group2TimeOpenHour = "00";
    String group3TimeOpenHour = "00";
    String group4TimeOpenHour = "00";
    String groupRadomTimeOpenHour = "00";

    String group1TimeOpenMin = "00";
    String group2TimeOpenMin = "00";
    String group3TimeOpenMin = "00";
    String group4TimeOpenMin = "00";
    String groupRadomTimeOpenMin = "00";


    String group1TimeCloseHour = "00";
    String group2TimeCloseHour = "00";
    String group3TimeCloseHour = "00";
    String group4TimeCloseHour = "00";
    String groupRadomTimeCloseHour = "00";

    String group1TimeCloseMin = "00";
    String group2TimeCloseMin = "00";
    String group3TimeCloseMin = "00";
    String group4TimeCloseMin = "00";
    String groupRadomTimeCloseMin = "00";


    String group1TimeClose = "11时20分";
    String group2TimeClose = "11时20分";
    String group3TimeClose = "11时20分";
    String group4TimeClose = "11时20分";
    String groupRadomTimeClose = "11时20分";

    long lastTime;
    long lastWaysTime;
    long lastProgressTime;
    int lastProgressNum;

    class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;  //消息(一个整型值)
                    mHandler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    //在主线程里面处理消息并更新UI界面
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    long sysTime = System.currentTimeMillis();//获取系统时间
                    CharSequence sysTimeStr = DateFormat.format("yyyy年MM月dd日 hh:mm:ss", sysTime);//时间显示格式
                    tv_control_time.setText(sysTimeStr); //更新时间
                    break;
                default:
                    break;

            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_control;
    }

    ArrayList<TextView> textViewList;
    ArrayList<CheckBox> checkBoxlist;
    ArrayList<ImageView> imageViewList;

    @Override
    public void initView() {
        //初始化分组
        textViewList = new ArrayList<>();
        checkBoxlist = new ArrayList<>();
        imageViewList = new ArrayList<>();
        textViewList.add(tv_light1);
        textViewList.add(tv_light2);
        textViewList.add(tv_light3);
        textViewList.add(tv_light4);
        textViewList.add(tv_light5);
        textViewList.add(tv_light6);
        textViewList.add(tv_light7);
        textViewList.add(tv_light8);
        textViewList.add(tv_light9);
        checkBoxlist.add(cb_lighr1);
        checkBoxlist.add(cb_lighr2);
        checkBoxlist.add(cb_lighr3);
        checkBoxlist.add(cb_lighr4);
        checkBoxlist.add(cb_lighr5);
        checkBoxlist.add(cb_lighr6);
        checkBoxlist.add(cb_lighr7);
        checkBoxlist.add(cb_lighr8);
        checkBoxlist.add(cb_lighr9);
        imageViewList.add(iv_light1);
        imageViewList.add(iv_light2);
        imageViewList.add(iv_light3);
        imageViewList.add(iv_light4);
        imageViewList.add(iv_light5);
        imageViewList.add(iv_light6);
        imageViewList.add(iv_light7);
        imageViewList.add(iv_light8);
        imageViewList.add(iv_light9);
        new TimeThread().start(); //启动新的线程
        //获取所有灯具信息
        getLampList();
        //获取分组信息
        getGroupData();
        //获取区域信息
        getRegion();
        //显示全部能耗信息
        getEnergyStatistics("0", true);


        tv_kdsj_time_hour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_kdsj_time_hour.isSelected()) {
                    tv_kdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_kdsj_time_hour.setSelected(false);
                } else {
                    tv_kdsj_time_hour.setTextColor(getResources().getColor(R.color.red));
                    tv_kdsj_time_hour.setSelected(true);
                    tv_kdsj_time_min.setSelected(false);
                    tv_kdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });


        tv_kdsj_time_min.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_kdsj_time_min.isSelected()) {
                    tv_kdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_kdsj_time_min.setSelected(false);
                } else {
                    tv_kdsj_time_min.setTextColor(getResources().getColor(R.color.red));
                    tv_kdsj_time_min.setSelected(true);
                    tv_kdsj_time_hour.setSelected(false);
                    tv_kdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });

        tv_gdsj_time_hour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_gdsj_time_hour.isSelected()) {
                    tv_gdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_gdsj_time_hour.setSelected(false);
                } else {
                    tv_gdsj_time_hour.setTextColor(getResources().getColor(R.color.red));
                    tv_gdsj_time_hour.setSelected(true);
                    tv_gdsj_time_min.setSelected(false);
                    tv_gdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });


        tv_gdsj_time_min.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_gdsj_time_min.isSelected()) {
                    tv_gdsj_time_min.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                    tv_gdsj_time_min.setSelected(false);
                } else {
                    tv_gdsj_time_min.setTextColor(getResources().getColor(R.color.red));
                    tv_gdsj_time_min.setSelected(true);
                    tv_gdsj_time_hour.setSelected(false);
                    tv_gdsj_time_hour.setTextColor(getResources().getColor(R.color.wmd_text_color_green));
                }
            }
        });


        rl_aab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                showTimePick1(tv_kdsj_time);
                if (tv_kdsj_time_hour.isSelected()) {
                    int hour = (Integer.valueOf(tv_kdsj_time_hour.getText().toString())) + 1;
                    if (hour < 10) {
                        tv_kdsj_time_hour.setText("0" + hour + "");
                    } else {
                        tv_kdsj_time_hour.setText(hour + "");
                    }
                    if (hour == 24) {
                        tv_kdsj_time_hour.setText("00");
                    }
                    lastWaysTime = System.currentTimeMillis();
                    saveTime();
                    refreashWays();
                }
                if (tv_kdsj_time_min.isSelected()) {
                    int hour = (Integer.valueOf(tv_kdsj_time_min.getText().toString())) + 15;
                    if (hour < 10) {
                        tv_kdsj_time_min.setText("0" + hour + "");
                    } else {
                        tv_kdsj_time_min.setText(hour + "");
                    }
                    if (hour == 60) {
                        tv_kdsj_time_min.setText("00");
                    }
                    lastWaysTime = System.currentTimeMillis();
                    saveTime();
                    refreashWays();
                }
            }
        });
        rl_aabb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_gdsj_time_hour.isSelected()) {
                    int hour = (Integer.valueOf(tv_gdsj_time_hour.getText().toString())) + 1;
                    if (hour < 10) {
                        tv_gdsj_time_hour.setText("0" + hour + "");
                    } else {
                        tv_gdsj_time_hour.setText(hour + "");
                    }
//                    tv_gdsj_time_hour.setText(hour + "");
                    if (hour == 24) {
                        tv_gdsj_time_hour.setText("00");
                    }
                    lastWaysTime = System.currentTimeMillis();
                    saveTime();
                    refreashWays();
                }
                if (tv_gdsj_time_min.isSelected()) {
                    int hour = (Integer.valueOf(tv_gdsj_time_min.getText().toString())) + 15;
                    if (hour < 10) {
                        tv_gdsj_time_min.setText("0" + hour + "");
                    } else {
                        tv_gdsj_time_min.setText(hour + "");
                    }

                    if (hour == 60) {
                        tv_gdsj_time_min.setText("00");
                    }
                    lastWaysTime = System.currentTimeMillis();
                    saveTime();
                    refreashWays();
                }
            }
        });
        rl_group_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rl_group_all.isSelected()) {

                } else {
                    showGroup(0);
                    showGroupLights(0);
                    if (isOpenGroupAll) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }
                    showSettingData(0);
                }
            }
        });

        rl_group1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rl_group1.isSelected()) {

                } else {
                    if (containsGroup("1")) {
                        showGroup(1);
                        showGroupLights(1);
                        if (isOpenGroup1) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                        showSettingData(1);
                    } else {
                        ToastUtil.showToast(ControlActivity.this, "组一未分组");
                    }

                }
            }
        });
        rl_group2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rl_group2.isSelected()) {

                } else {
                    if (containsGroup("2")) {
                        showGroup(2);
                        showGroupLights(2);
                        if (isOpenGroup2) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                        showSettingData(2);
                    } else {
                        ToastUtil.showToast(ControlActivity.this, "组二未分组");
                    }
                }
            }
        });
        rl_group3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rl_group3.isSelected()) {

                } else {
                    if (containsGroup("3")) {
                        showGroup(3);
                        showGroupLights(3);
                        if (isOpenGroup3) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                        showSettingData(3);
                    } else {
                        ToastUtil.showToast(ControlActivity.this, "组三未分组");
                    }
                }
            }
        });
        rl_group_radom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rl_group_radom.isSelected()) {

                } else {
                    showGroup(4);
                    showGroupLights(4);
                    if (isOpenLight1 || isOpenLight2 || isOpenLight3 || isOpenLight4 || isOpenLight5 || isOpenLight6 || isOpenLight7 || isOpenLight8 || isOpenLight9) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }
                    showSettingData(4);
                }
            }
        });
        iv_group_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rl_group_all.isSelected()) {
                    if (containsGroup("1")) {
                        showGroup(1);
                        showGroupLights(1);
                        showSettingData(1);
                        if (isOpenGroupAll) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                    } else {
                        ToastUtil.showToast(ControlActivity.this, "组一未分组");
                    }
                } else if (rl_group1.isSelected()) {
                    if (containsGroup("2")) {
                        showGroup(2);
                        showGroupLights(2);
                        showSettingData(2);
                        if (isOpenGroup1) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                    } else {
                        ToastUtil.showToast(ControlActivity.this, "组二未分组");
                    }
                } else if (rl_group2.isSelected()) {
                    if (containsGroup("3")) {
                        showGroup(3);
                        showGroupLights(3);
                        showSettingData(3);
                        if (isOpenGroup2) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                    } else {
                        ToastUtil.showToast(ControlActivity.this, "组三未分组");
                    }
                } else if (rl_group3.isSelected()) {

                    showGroupLights(4);
                    showGroup(4);
                    showSettingData(4);
                    if (isOpenGroup3) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }

                } else if (rl_group_radom.isSelected()) {
                    showGroupLights(0);
                    showGroup(0);
                    showSettingData(0);
                    if (isOpenLight1 || isOpenLight2 || isOpenLight3 || isOpenLight4 || isOpenLight5 || isOpenLight6 || isOpenLight7 || isOpenLight8 || isOpenLight9) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }
                } else {
                    showGroupLights(0);
                    showGroup(0);
                    showSettingData(0);
                    if (isOpenGroupAll) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }
                }
            }
        });
        //灯光打开调用
        iv_light_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (System.currentTimeMillis() - lastTime > 2000) {
                    if (iv_light_open.isSelected()) {
//                    iv_light_open.setSelected(false);
                        if (rl_group_all.isSelected()) {
//                        ToastUtils.showToast(ControlActivity.this, "全局灯光关闭");
//                        isOpenGroupAll = false;
                            ArrayList<LampListBean> list = new ArrayList<>();
                            for (LampListBean bean : lampList) {
                                bean.setIsOpen("0");
                                list.add(bean);
                            }
                            openOrCloseConbinationGroup(list, "全局灯光关闭", false, true);

                            add4TimeAndMan(getGroupId(0));
                        } else if (rl_group1.isSelected() && containsGroup("1")) {
//                        ToastUtils.showToast(ControlActivity.this, "组一灯光关闭");
//                        isOpenGroup1 = false;
                            lightOpenAll(0, getGroupId(0), "组一灯光关闭", 1);
                            add4TimeAndMan(getGroupId(0));
                        } else if (rl_group2.isSelected() && containsGroup("2")) {
//                        ToastUtils.showToast(ControlActivity.this, "组二灯光关闭");
//                        isOpenGroup2 = false;
                            lightOpenAll(0, getGroupId(1), "组二灯光关闭", 2);
                            add4TimeAndMan(getGroupId(1));
                        } else if (rl_group3.isSelected() && containsGroup("3")) {
//                        ToastUtils.showToast(ControlActivity.this, "组三灯光关闭");
//                        isOpenGroup3 = false;
                            lightOpenAll(0, getGroupId(2), "组三灯光关闭", 3);
                            add4TimeAndMan(getGroupId(2));
                        } else if (rl_group_radom.isSelected()) {
                            ToastUtils.showToast(ControlActivity.this, "任意分组灯光关闭");
//                        isOpenGroupRandom = true;
                            ArrayList<LampListBean> list = new ArrayList<>();
                            for (int i = 0; i < 9; i++) {
                                if (checkBoxlist.get(i).isChecked()) {
                                    LampListBean bean = lampList.get(i);
                                    bean.setIsOpen("0");
                                    list.add(bean);
                                }

                            }
                            if (list.size() == 0) {
                                ToastUtils.showToast(ControlActivity.this, "未勾选灯组");
                            } else {
                                openOrCloseConbinationGroup(list, "任意分组灯光关闭", false, false);
                            }
                        } else {
                            ToastUtils.showToast(ControlActivity.this, "请选择分组");
                        }

                    } else {
//                    iv_light_open.setSelected(true);
                        if (rl_group_all.isSelected()) {
//                        ToastUtils.showToast(ControlActivity.this, "全局灯光打开");
//                        isOpenGroupAll = true;
                            ArrayList<LampListBean> list = new ArrayList<>();
                            for (LampListBean bean : lampList) {
                                bean.setIsOpen("1");
                                list.add(bean);
                            }
                            openOrCloseConbinationGroup(list, "全局灯光打开", true, true);

                        } else if (rl_group1.isSelected() && containsGroup("1")) {
//                        ToastUtils.showToast(ControlActivity.this, "组一灯光打开");
//                        isOpenGroup1 = true;
                            lightOpenAll(1, getGroupId(0), "组一灯光打开", 1);
                        } else if (rl_group2.isSelected() && containsGroup("2")) {
//                        ToastUtils.showToast(ControlActivity.this, "组二灯光打开");
//                        isOpenGroup2 = true;
                            lightOpenAll(1, getGroupId(1), "组二灯光打开", 2);
                        } else if (rl_group3.isSelected() && containsGroup("3")) {
//                        ToastUtils.showToast(ControlActivity.this, "组三灯光打开");
//                        isOpenGroup3 = true;
                            lightOpenAll(1, getGroupId(2), "组三灯光打开", 3);
                        } else if (rl_group_radom.isSelected()) {
//                        ToastUtils.showToast(ControlActivity.this, "任意组灯光打开");
//                        isOpenGroupRandom = true;
                            ArrayList<LampListBean> list = new ArrayList<>();
                            for (int i = 0; i < 9; i++) {
                                if (checkBoxlist.get(i).isChecked()) {
                                    LampListBean bean = lampList.get(i);
                                    bean.setIsOpen("1");
                                    list.add(bean);
                                }

                            }
                            if (list.size() == 0) {
                                ToastUtils.showToast(ControlActivity.this, "未勾选灯组");
                            } else {
                                openOrCloseConbinationGroup(list, "任意组灯光打开", true, false);
                            }
                        } else {
                            ToastUtils.showToast(ControlActivity.this, "请选择分组");
                        }
//                    setLightState(true);
                        lastTime = System.currentTimeMillis();
                    }
                } else {
                    ToastUtils.showToast(ControlActivity.this, "请勿频繁执行开关灯操作");

                }


            }
        });

        iv_group_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rl_group_all.isSelected()) {
                    showGroup(4);
                    showGroupLights(4);
                    showSettingData(4);
                    if (isOpenGroupAll) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }
                } else if (rl_group1.isSelected()) {

                    showGroup(0);
                    showGroupLights(0);
                    showSettingData(0);
                    if (isOpenGroup1) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }
                } else if (rl_group2.isSelected()) {
                    if (containsGroup("1")) {
                        showGroup(1);
                        showGroupLights(1);
                        showSettingData(1);
                        if (isOpenGroup2) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                    }
                } else if (rl_group3.isSelected()) {
                    if (containsGroup("2")) {
                        showGroupLights(2);
                        showGroup(2);
                        showSettingData(2);
                        if (isOpenGroup3) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                    }
                } else if (rl_group_radom.isSelected()) {
                    if (containsGroup("3")) {
                        showGroupLights(3);
                        showGroup(3);
                        showSettingData(3);
                        if (isOpenLight1 || isOpenLight2 || isOpenLight3 || isOpenLight4 || isOpenLight5 || isOpenLight6 || isOpenLight7 || isOpenLight8 || isOpenLight9) {
                            iv_light_open.setSelected(true);
                        } else {
                            iv_light_open.setSelected(false);
                        }
                    }
                } else {
                    showGroupLights(0);
                    showGroup(0);
                    showSettingData(0);
                    if (isOpenGroupAll) {
                        iv_light_open.setSelected(true);
                    } else {
                        iv_light_open.setSelected(false);
                    }
                }
            }

        });

        //持续时间
        iv_jbsj_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.valueOf(tv_jbsj_time.getText().toString()) + 1;
                if (num == 6) {
                    num = 5;
                    ToastUtil.showToast(ControlActivity.this, "已到达上限");
                    tv_jbsj_time.setText(num + "");

                    if (rl_group_all.isSelected()) {
                        jbsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        jbsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        jbsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        jbsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        jbsjGroupRadom = num;
                    }
                } else {
                    lastWaysTime = System.currentTimeMillis();
                    refreashWays();
                    tv_jbsj_time.setText(num + "");

                    if (rl_group_all.isSelected()) {
                        jbsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        jbsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        jbsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        jbsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        jbsjGroupRadom = num;
                    }
                }


            }
        });

        iv_jbsj_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.valueOf(tv_jbsj_time.getText().toString()) - 1;
                if (num == 0) {
                    num = 1;
                    ToastUtil.showToast(ControlActivity.this, "已到达下限");
                    if (num > 0) {
                        tv_jbsj_time.setText(num + "");
                    }
                    if (rl_group_all.isSelected()) {
                        jbsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        jbsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        jbsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        jbsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        jbsjGroupRadom = num;
                    }
                } else {
                    lastWaysTime = System.currentTimeMillis();
                    refreashWays();
                    if (num > 0) {
                        tv_jbsj_time.setText(num + "");
                    }
                    if (rl_group_all.isSelected()) {
                        jbsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        jbsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        jbsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        jbsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        jbsjGroupRadom = num;
                    }
                }


            }
        });

        //持续时间
        iv_cxsj_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.valueOf(tv_cxsj_time.getText().toString()) + 1;
                if (num == 31) {
                    num = 30;
                    ToastUtil.showToast(ControlActivity.this, "已到达上限");

                    tv_cxsj_time.setText(num + "");
                    if (rl_group_all.isSelected()) {
                        cxsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        cxsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        cxsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        cxsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        cxsjGroupRadom = num;
                    }
                } else {
                    lastWaysTime = System.currentTimeMillis();
                    refreashWays();

                    tv_cxsj_time.setText(num + "");
                    if (rl_group_all.isSelected()) {
                        cxsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        cxsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        cxsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        cxsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        cxsjGroupRadom = num;
                    }
                }

            }
        });

        iv_cxsj_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.valueOf(tv_cxsj_time.getText().toString()) - 1;

                if (num == 4) {
                    num = 5;
                    ToastUtil.showToast(ControlActivity.this, "已到达下限");
                    if (num > 0) {
                        tv_cxsj_time.setText(num + "");
                    }
                    if (rl_group_all.isSelected()) {
                        cxsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        cxsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        cxsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        cxsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        cxsjGroupRadom = num;
                    }
                } else {
                    lastWaysTime = System.currentTimeMillis();
                    refreashWays();
                    if (num > 0) {
                        tv_cxsj_time.setText(num + "");
                    }
                    if (rl_group_all.isSelected()) {
                        cxsjGroup1 = num;
                    } else if (rl_group1.isSelected()) {
                        cxsjGroup2 = num;
                    } else if (rl_group2.isSelected()) {
                        cxsjGroup3 = num;
                    } else if (rl_group3.isSelected()) {
                        cxsjGroup4 = num;
                    } else if (rl_group_radom.isSelected()) {
                        cxsjGroupRadom = num;
                    }
                }


            }
        });

        view_pg.setProgress(5);
        view_pg.setOnProgressChangedListener(new TouchProgessView.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(View view, int progress) {
                if (progress < 5) {
                    view_pg.setProgress(5);
                    Log.d("TouchProgessView", "view_pg.setProgress(5)");
//                    this.onProgressSuccess(view,progress);
                }
            }

            @Override
            public void onProgressSuccess(View view, int progress) {
                int num = 0;
                if (System.currentTimeMillis() - lastProgressTime > 1000) {

                    num = progress;
                    if (progress < 5) {
                        view_pg.setProgress(5);
                        num = 5;
                    }
                    if (progress > 100) {
                        view_pg.setProgress(100);
                        num = 100;
                    }
                    Log.d("TouchProgessView", "onProgressSuccess = " + num);
                    if (rl_group_all.isSelected()) {
                        ArrayList<LampListBean> list = new ArrayList<>();
                        for (LampListBean bean : lampList) {
                            bean.setLight(num + "");
                            list.add(bean);
                        }
                        brightnessConbinationGroup(list);
                        group1Progress = num;
                    } else if (rl_group1.isSelected()) {
                        brightnessAll(num, getGroupId(0));
                        group2Progress = num;
                    } else if (rl_group2.isSelected()) {
                        brightnessAll(num, getGroupId(1));
                        group3Progress = num;
                    } else if (rl_group3.isSelected()) {
                        brightnessAll(num, getGroupId(2));
                        group4Progress = num;
                    } else if (rl_group_radom.isSelected()) {
                        ArrayList<LampListBean> list = new ArrayList<>();
                        for (int i = 0; i < 9; i++) {
                            if (checkBoxlist.get(i).isChecked()) {
                                LampListBean bean = lampList.get(i);
                                bean.setLight(num + "");
                                list.add(bean);
                            }

                        }
                        brightnessConbinationGroup(list);
                        group5Progress = num;
                    }
                    lastProgressNum = num;
                } else {
                    ToastUtil.showToast(ControlActivity.this, "亮度调节频繁，请稍后再试");
                    view_pg.setProgress(lastProgressNum);
                }

//                Toast.makeText(ControlActivity.this, "当前亮度已调节为" + progress, Toast.LENGTH_SHORT).show();
                lastProgressTime = System.currentTimeMillis();

            }
        });


        bassController.circlePaint2.setColor(themeColor);
        bassController.linePaint.setColor(themeColor);
        bassController.invalidate();
        bassController.linePaint.setColor(themeColor);
        bassController.setProgress(1);


        bassController2.circlePaint2.setColor(themeColor);
        bassController2.linePaint.setColor(themeColor);
        bassController2.invalidate();
        bassController2.linePaint.setColor(themeColor);
        bassController2.setProgress(1);

        bassController2.setOnProgressChangedListener(new AnalogController.onProgressChangedListener() {
            @Override
            public void onProgressChanged(int progress) {

                int num;
                if (rl_group_all.isSelected()) {

                    group1Down = Integer.valueOf(bassController2.getAngel());
                    if (group1Up < group1Down) {
                        group1Up = Integer.valueOf(bassController2.getAngel());
                        bassController.setProgress(group1Up);
                    }


                } else if (rl_group1.isSelected()) {

                    group2Down = Integer.valueOf(bassController2.getAngel());
                    if (group2Up < group2Down) {
                        group2Up = Integer.valueOf(bassController2.getAngel());
                        bassController.setProgress(group2Up);
                    }

                } else if (rl_group2.isSelected()) {
                    group3Down = Integer.valueOf(bassController2.getAngel());
                    if (group3Up < group3Down) {
                        group3Up = Integer.valueOf(bassController2.getAngel());
                        bassController.setProgress(group3Up);
                    }
                } else if (rl_group3.isSelected()) {
                    group4Down = Integer.valueOf(bassController2.getAngel());
                    if (group4Up < group4Down) {
                        group4Up = Integer.valueOf(bassController2.getAngel());
                        bassController.setProgress(group4Up);
                    }
                } else if (rl_group_radom.isSelected()) {
                    groupRadomDown = Integer.valueOf(bassController2.getAngel());
                    if (groupRadomUp < groupRadomDown) {
                        groupRadomUp = Integer.valueOf(bassController2.getAngel());
                        bassController.setProgress(groupRadomUp);
                    }
                }
                lastWaysTime = System.currentTimeMillis();
                refreashWays();
            }
        });


        bassController.setOnProgressChangedListener(new AnalogController.onProgressChangedListener() {
            @Override
            public void onProgressChanged(int progress) {
                if (rl_group_all.isSelected()) {
                    group1Up = Integer.valueOf(bassController.getAngel());
                    if (group1Down > group1Up) {
                        bassController.setProgress(group1Down);
                    }
                } else if (rl_group1.isSelected()) {
                    group2Up = Integer.valueOf(bassController.getAngel());
                    if (group2Down > group2Up) {
                        bassController.setProgress(group2Down);
                    }
                } else if (rl_group2.isSelected()) {
                    group3Up = Integer.valueOf(bassController.getAngel());
                    if (group3Down > group3Up) {
                        bassController.setProgress(group3Down);
                    }
                } else if (rl_group3.isSelected()) {
                    group4Up = Integer.valueOf(bassController.getAngel());
                    if (group4Down > group4Up) {
                        bassController.setProgress(group4Down);
                    }
                } else if (rl_group_radom.isSelected()) {
                    groupRadomUp = Integer.valueOf(bassController.getAngel());
                    if (groupRadomDown > groupRadomUp) {
                        bassController.setProgress(groupRadomDown);
                    }
                }
                lastWaysTime = System.currentTimeMillis();
                refreashWays();
            }
        });
        iv_control_power_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ControlActivity.this);
                //给对话框添加内容
                builder.setMessage("是否确定退出登陆");
                //给对话框添加点击事件

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        Log.e("对话框", "取消 which = " + which);
                    }
                });

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtil.showToast(ControlActivity.this, "登出成功");
                        AppActivities.instance().finishTo(ControlActivity.this, LoginActivity.class);
                    }
                });
                //切记勿忘~开启dialog
                builder.show();
            }
        });

        ArrayList<Integer> timeList = new ArrayList<>();
        timeList.add(1000);
        timeList.add(1500);
        timeList.add(1200);
        timeList.add(2200);
        timeList.add(2600);
        timeList.add(1900);
        timeList.add(4000);
        timeList.add(1200);
        ArrayList<String> bottomList = new ArrayList<>();
        bottomList.add("1.10");
        bottomList.add("1.11");
        bottomList.add("1.12");
        bottomList.add("1.13");
        bottomList.add("1.14");
        bottomList.add("1.15");
        bottomList.add("1.16");
        bottomList.add("1.17");
        view1.updateTime(timeList, bottomList);
    }


    public void setLightState(boolean a) {
        if (cb_lighr1.isChecked()) {
            if (a) {
                isOpenLight1 = true;

            } else {
                isOpenLight1 = false;
            }
        }

        if (cb_lighr2.isChecked()) {
            if (a) {
                isOpenLight2 = true;
            } else {
                isOpenLight2 = false;
            }
        }

        if (cb_lighr3.isChecked()) {
            if (a) {
                isOpenLight3 = true;
            } else {
                isOpenLight3 = false;
            }
        }

        if (cb_lighr4.isChecked()) {
            if (a) {
                isOpenLight4 = true;
            } else {
                isOpenLight4 = false;
            }
        }

        if (cb_lighr5.isChecked()) {
            if (a) {
                isOpenLight5 = true;
            } else {
                isOpenLight5 = false;
            }
        }

        if (cb_lighr6.isChecked()) {
            if (a) {
                isOpenLight6 = true;
            } else {
                isOpenLight6 = false;
            }
        }

        if (cb_lighr7.isChecked()) {
            if (a) {
                isOpenLight7 = true;
            } else {
                isOpenLight7 = false;
            }
        }

        if (cb_lighr8.isChecked()) {
            if (a) {
                isOpenLight8 = true;
            } else {
                isOpenLight8 = false;
            }
        }

        if (cb_lighr9.isChecked()) {
            if (a) {
                isOpenLight9 = true;
            } else {
                isOpenLight9 = false;
            }
        }

    }

    public void showLightState() {
        if (isOpenLight1) {
            iv_light1.setSelected(true);
        } else {
            iv_light1.setSelected(false);
        }

        if (isOpenLight2) {
            iv_light2.setSelected(true);
        } else {
            iv_light2.setSelected(false);
        }

        if (isOpenLight3) {
            iv_light3.setSelected(true);
        } else {
            iv_light3.setSelected(false);
        }

        if (isOpenLight4) {
            iv_light4.setSelected(true);
        } else {
            iv_light4.setSelected(false);
        }

        if (isOpenLight5) {
            iv_light5.setSelected(true);
        } else {
            iv_light5.setSelected(false);
        }

        if (isOpenLight6) {
            iv_light6.setSelected(true);
        } else {
            iv_light6.setSelected(false);
        }

        if (isOpenLight7) {
            iv_light7.setSelected(true);
        } else {
            iv_light7.setSelected(false);
        }

        if (isOpenLight8) {
            iv_light8.setSelected(true);
        } else {
            iv_light8.setSelected(false);
        }

        if (isOpenLight9) {
            iv_light9.setSelected(true);
        } else {
            iv_light9.setSelected(false);
        }
    }

    public void showLightCheckBox(int state) {
        cb_lighr1.setChecked(false);
        cb_lighr2.setChecked(false);
        cb_lighr3.setChecked(false);
        cb_lighr4.setChecked(false);
        cb_lighr5.setChecked(false);
        cb_lighr6.setChecked(false);
        cb_lighr7.setChecked(false);
        cb_lighr8.setChecked(false);
        cb_lighr9.setChecked(false);
        cb_lighr1.setClickable(false);
        cb_lighr2.setClickable(false);
        cb_lighr3.setClickable(false);
        cb_lighr4.setClickable(false);
        cb_lighr5.setClickable(false);
        cb_lighr6.setClickable(false);
        cb_lighr7.setClickable(false);
        cb_lighr8.setClickable(false);
        cb_lighr9.setClickable(false);
        switch (state) {
            case 0:
                cb_lighr1.setChecked(true);
                cb_lighr2.setChecked(true);
                cb_lighr3.setChecked(true);
                cb_lighr4.setChecked(true);
                cb_lighr5.setChecked(true);
                cb_lighr6.setChecked(true);
                cb_lighr7.setChecked(true);
                cb_lighr8.setChecked(true);
                cb_lighr9.setChecked(true);
                cb_lighr1.setClickable(false);
                cb_lighr2.setClickable(false);
                cb_lighr3.setClickable(false);
                cb_lighr4.setClickable(false);
                cb_lighr5.setClickable(false);
                cb_lighr6.setClickable(false);
                cb_lighr7.setClickable(false);
                cb_lighr8.setClickable(false);
                cb_lighr9.setClickable(false);

                break;
            case 1:
                if (containsGroup("1")) {
                    for (LampListBean bean : lampList) {
                        if (bean.getGroupId() == 1) {
                            checkBoxlist.get(Integer.valueOf(bean.getSerialNum())).setChecked(true);
                        }
                    }
                }
                break;
            case 2:
                if (containsGroup("2")) {
                    for (LampListBean bean : lampList) {
                        if (bean.getGroupId() == 2) {
                            checkBoxlist.get(Integer.valueOf(bean.getSerialNum())).setChecked(true);
                        }
                    }
                }
                break;
            case 3:
                if (containsGroup("3")) {
                    for (LampListBean bean : lampList) {
                        if (bean.getGroupId() == 3) {
                            checkBoxlist.get(Integer.valueOf(bean.getSerialNum())).setChecked(true);
                        }
                    }
                }
                break;
            case 4:
                cb_lighr1.setChecked(false);
                cb_lighr2.setChecked(false);
                cb_lighr3.setChecked(false);
                cb_lighr4.setChecked(false);
                cb_lighr5.setChecked(false);
                cb_lighr6.setChecked(false);
                cb_lighr7.setChecked(false);
                cb_lighr8.setChecked(false);
                cb_lighr9.setChecked(false);
                cb_lighr1.setClickable(true);
                cb_lighr2.setClickable(true);
                cb_lighr3.setClickable(true);
                cb_lighr4.setClickable(true);
                cb_lighr5.setClickable(true);
                cb_lighr6.setClickable(true);
                cb_lighr7.setClickable(true);
                cb_lighr8.setClickable(true);
                cb_lighr9.setClickable(true);
                break;
        }
    }

    public void showGroupLights(int state) {
        showLightCheckBox(state);
        showLightState();

    }

    public void showGroup(int state) {
        showLightCheckBox(state);
        switch (state) {
            case 0:
                rl_group_all.setSelected(true);
                rl_group1.setSelected(false);
                rl_group2.setSelected(false);
                rl_group3.setSelected(false);
                rl_group_radom.setSelected(false);
                tv_group_all.setTextColor(getResources().getColor(R.color.white));
                tv_group1.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group2.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group3.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group_radom.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_lable_all.setText("全局");
                getEnergyStatistics("0", true);
                break;
            case 1:
                rl_group_all.setSelected(false);
                rl_group1.setSelected(true);
                rl_group2.setSelected(false);
                rl_group3.setSelected(false);
                rl_group_radom.setSelected(false);
                tv_group_all.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group1.setTextColor(getResources().getColor(R.color.white));
                tv_group2.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group3.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group_radom.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_lable_all.setText("组一");
                getEnergyStatistics(getGroupId(0), false);
                break;
            case 2:
                rl_group_all.setSelected(false);
                rl_group1.setSelected(false);
                rl_group2.setSelected(true);
                rl_group3.setSelected(false);
                rl_group_radom.setSelected(false);
                tv_group_all.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group1.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group2.setTextColor(getResources().getColor(R.color.white));
                tv_group3.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group_radom.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_lable_all.setText("组二");
                getEnergyStatistics(getGroupId(1), false);
                break;
            case 3:
                rl_group_all.setSelected(false);
                rl_group1.setSelected(false);
                rl_group2.setSelected(false);
                rl_group3.setSelected(true);
                rl_group_radom.setSelected(false);
                tv_group_all.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group1.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group2.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group3.setTextColor(getResources().getColor(R.color.white));
                tv_group3.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group_radom.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_lable_all.setText("组三");
                getEnergyStatistics(getGroupId(2), false);
                break;
            case 4:
                rl_group_all.setSelected(false);
                rl_group1.setSelected(false);
                rl_group2.setSelected(false);
                rl_group3.setSelected(false);
                rl_group_radom.setSelected(true);
                tv_group_all.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group1.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group2.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group3.setTextColor(getResources().getColor(R.color.wmd_text_color));
                tv_group_radom.setTextColor(getResources().getColor(R.color.white));
                tv_lable_all.setText("任意");
                getEnergyStatistics("0", true);
                break;
        }

    }

    public void showSettingData(int state) {
        switch (state) {
            case 0:
                tv_jbsj_time.setText(jbsjGroup1 + "");
                tv_cxsj_time.setText(cxsjGroup1 + "");
                bassController.setProgress(group1Up);
                bassController2.setProgress(group1Down);

                tv_gdsj_time_hour.setText(group1TimeCloseHour);
                tv_gdsj_time_min.setText(group1TimeCloseMin);
                tv_kdsj_time_hour.setText(group1TimeOpenHour);
                tv_kdsj_time_min.setText(group1TimeOpenMin);
                view_pg.setProgress(group1Progress);
                break;
            case 1:
                tv_jbsj_time.setText(jbsjGroup2 + "");
                tv_cxsj_time.setText(cxsjGroup2 + "");
                bassController.setProgress(group2Up);
                bassController2.setProgress(group2Down);

                tv_gdsj_time_hour.setText(group2TimeCloseHour);
                tv_gdsj_time_min.setText(group2TimeCloseMin);
                tv_kdsj_time_hour.setText(group2TimeOpenHour);
                tv_kdsj_time_min.setText(group2TimeOpenMin);
                view_pg.setProgress(group2Progress);
                break;
            case 2:
                tv_jbsj_time.setText(jbsjGroup3 + "");
                tv_cxsj_time.setText(cxsjGroup3 + "");
                bassController.setProgress(group3Up);
                bassController2.setProgress(group3Down);
                tv_gdsj_time_hour.setText(group3TimeCloseHour);
                tv_gdsj_time_min.setText(group3TimeCloseMin);
                tv_kdsj_time_hour.setText(group3TimeOpenHour);
                tv_kdsj_time_min.setText(group3TimeOpenMin);
                view_pg.setProgress(group3Progress);
                break;
            case 3:
                tv_jbsj_time.setText(jbsjGroup4 + "");
                tv_cxsj_time.setText(cxsjGroup4 + "");
                bassController.setProgress(group4Up);
                bassController2.setProgress(group4Down);
                tv_gdsj_time_hour.setText(group4TimeCloseHour);
                tv_gdsj_time_min.setText(group4TimeCloseMin);
                tv_kdsj_time_hour.setText(group4TimeOpenHour);
                tv_kdsj_time_min.setText(group4TimeOpenMin);
                view_pg.setProgress(group4Progress);
                break;
            case 4:
                tv_jbsj_time.setText(jbsjGroupRadom + "");
                tv_cxsj_time.setText(cxsjGroupRadom + "");
                bassController.setProgress(groupRadomUp);
                bassController2.setProgress(groupRadomDown);
                tv_gdsj_time_hour.setText(groupRadomTimeCloseHour);
                tv_gdsj_time_min.setText(groupRadomTimeCloseMin);
                tv_kdsj_time_hour.setText(groupRadomTimeOpenHour);
                tv_kdsj_time_min.setText(groupRadomTimeOpenMin);
                view_pg.setProgress(group5Progress);
                break;
        }
        showTime();

    }

    private void showTime() {
        if (rl_group_all.isSelected()) {
            tv_kdsj_time_hour.setText(group1TimeOpenHour);
            tv_kdsj_time_min.setText(group1TimeOpenMin);
            tv_gdsj_time_hour.setText(group1TimeCloseHour);
            tv_gdsj_time_min.setText(group1TimeCloseMin);
        }

        if (rl_group1.isSelected()) {
            tv_kdsj_time_hour.setText(group2TimeOpenHour);
            tv_kdsj_time_min.setText(group2TimeOpenMin);
            tv_gdsj_time_hour.setText(group2TimeCloseHour);
            tv_gdsj_time_min.setText(group2TimeCloseMin);
        }

        if (rl_group2.isSelected()) {
            tv_kdsj_time_hour.setText(group3TimeOpenHour);
            tv_kdsj_time_min.setText(group3TimeOpenMin);
            tv_gdsj_time_hour.setText(group3TimeCloseHour);
            tv_gdsj_time_min.setText(group3TimeCloseMin);
        }

        if (rl_group3.isSelected()) {
            tv_kdsj_time_hour.setText(group4TimeOpenHour);
            tv_kdsj_time_min.setText(group4TimeOpenMin);
            tv_gdsj_time_hour.setText(group4TimeCloseHour);
            tv_gdsj_time_min.setText(group4TimeCloseMin);
        }
        if (rl_group_radom.isSelected()) {
            tv_kdsj_time_hour.setText(groupRadomTimeOpenHour);
            tv_kdsj_time_min.setText(groupRadomTimeOpenMin);
            tv_gdsj_time_hour.setText(groupRadomTimeCloseHour);
            tv_gdsj_time_min.setText(groupRadomTimeCloseMin);
        }
    }


    private void saveTime() {
        if (rl_group_all.isSelected()) {
            group1TimeOpenHour = tv_kdsj_time_hour.getText().toString();
            group1TimeOpenMin = tv_kdsj_time_min.getText().toString();
            group1TimeCloseHour = tv_gdsj_time_hour.getText().toString();
            group1TimeCloseMin = tv_gdsj_time_min.getText().toString();
        }

        if (rl_group1.isSelected()) {
            group2TimeOpenHour = tv_kdsj_time_hour.getText().toString();
            group2TimeOpenMin = tv_kdsj_time_min.getText().toString();
            group2TimeCloseHour = tv_gdsj_time_hour.getText().toString();
            group2TimeCloseMin = tv_gdsj_time_min.getText().toString();
        }

        if (rl_group2.isSelected()) {
            group3TimeOpenHour = tv_kdsj_time_hour.getText().toString();
            group3TimeOpenMin = tv_kdsj_time_min.getText().toString();
            group3TimeCloseHour = tv_gdsj_time_hour.getText().toString();
            group3TimeCloseMin = tv_gdsj_time_min.getText().toString();
        }

        if (rl_group3.isSelected()) {
            group4TimeOpenHour = tv_kdsj_time_hour.getText().toString();
            group4TimeOpenMin = tv_kdsj_time_min.getText().toString();
            group4TimeCloseHour = tv_gdsj_time_hour.getText().toString();
            group4TimeCloseMin = tv_gdsj_time_min.getText().toString();
        }
        if (rl_group_radom.isSelected()) {
            groupRadomTimeOpenHour = tv_kdsj_time_hour.getText().toString();
            groupRadomTimeOpenMin = tv_kdsj_time_min.getText().toString();
            groupRadomTimeCloseHour = tv_gdsj_time_hour.getText().toString();
            groupRadomTimeCloseMin = tv_gdsj_time_min.getText().toString();
        }
    }

    private void showTimePick2(TextView btn) {


        TimePickerView pvTime = new TimePickerView.Builder(ControlActivity.this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat("HH时mm分");
                String datef = formatter.format(date);
                btn.setText(datef);
                if (rl_group_all.isSelected()) {
                    group1TimeClose = datef;
                } else if (rl_group1.isSelected()) {
                    group2TimeClose = datef;
                } else if (rl_group2.isSelected()) {
                    group3TimeClose = datef;
                } else if (rl_group3.isSelected()) {
                    group4TimeClose = datef;
                } else if (rl_group_radom.isSelected()) {
                    groupRadomTimeClose = datef;
                }
            }
        })
                .setType(new boolean[]{false, false, false, true, true, false})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字

                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();

        pvTime.show();

    }


    int port;
    String host;
    ArrayList<GroupBean> groupList;

    public String getGroupId(int state) {
        if (groupList != null && groupList.size() > state) {
            return groupList.get(state).getGroupId();
        } else {
            return "";
        }
    }


    public boolean containsGroup(String state) {
        boolean isExsit = false;
        if (groupList.size() != 0) {
            for (GroupBean bean : groupList) {
                if (bean.getGroupId().equals(state)) {
                    isExsit = true;
                } else {

                }
            }
        }
        return isExsit;
    }

    //获取灯具分组
    private void getGroupData() {
        try {

            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<>();
//            params.put("regionId", nowRegion.getValue());
            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).getLightGroupNew(token, params).enqueue(new Callback<AuthBaseBean<ArrayList<GroupBean>>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<ArrayList<GroupBean>>> call, Response<AuthBaseBean<ArrayList<GroupBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if (response.body() != null && response.body().getData() != null) {
                        groupList = response.body().getData();
                        ToastUtil.showToast(ControlActivity.this, "灯具分组信息获取成功");
                    }

                }

                @Override
                public void onFailure(Call<AuthBaseBean<ArrayList<GroupBean>>> call, Throwable t) {
                    SCLog.e(TAG, "灯组信息获取失败" + call.toString() + t.getMessage());

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    //控制灯光打开关闭
    private void lightOpenAll(int isOpen, String groupId, String message, int groupNum) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<>();
            params.put("groupId", groupId);
            params.put("isOpen", isOpen + "");

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new JSONObject(params).toString());

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).lightOpenAll(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity.this, message, Toast.LENGTH_SHORT).show();
                    if (isOpen == 1) {
                        iv_light_open.setSelected(true);
                        setLightState(true);
                        if (groupNum == 1) {
                            isOpenGroup1 = true;
                        } else if (groupNum == 2) {
                            isOpenGroup2 = true;
                        } else if (groupNum == 3) {
                            isOpenGroup3 = true;
                        }
                    } else {
                        iv_light_open.setSelected(false);
                        setLightState(false);
                        if (groupNum == 1) {
                            isOpenGroup1 = false;
                        } else if (groupNum == 2) {
                            isOpenGroup2 = false;
                        } else if (groupNum == 3) {
                            isOpenGroup3 = false;
                        }
                    }
                    showLightState();
                    saveTime();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "组灯光操作失败" + t.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //控制灯光亮度
    private void brightnessAll(int level, String groupId) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<String, String>();
            params.put("groupId", groupId);
            params.put("light", level + "");

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new JSONObject(params).toString());

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).brightnessAll(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity.this, "第" + groupId + "组灯光亮度调节" + level + "成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "第" + groupId + "组灯光亮度调节" + level + "失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    //设置生效策略

    private void add4TimeAndMan(String groupId) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            BindingBean parms = new BindingBean();
            parms.setBindingState(1);
            parms.setManState(1);
            if (rl_group_all.isSelected()) {
                parms.setRegionId(nowRegion.getValue());
                parms.setType(0);
            } else {
                parms.setType(1);
                parms.setGrpId(groupId);
            }


            parms.setMan(getMan());
            parms.setTime(getTime());
            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new Gson().toJson(parms));

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).add4TimeAndMan(token, requestBody).enqueue(new Callback<AuthBaseBean<BindingBean>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<BindingBean>> call, Response<AuthBaseBean<BindingBean>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
//                    Toast.makeText(ControlActivity.this, "第" + groupId + "策略生效成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<BindingBean>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "第" + groupId + "策略生效失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    //修改接口
    private void editAdd4TimeAndMan(String groupId) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            BindingBean parms = new BindingBean();
            parms.setBindingState(1);
            parms.setManState(1);
            if (rl_group_all.isSelected()) {
                parms.setRegionId(nowRegion.getValue());
                parms.setType(0);
            } else {
                parms.setType(1);
                parms.setGrpId(groupId);
            }


            parms.setMan(getMan());
            parms.setTime(getTime());
            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new Gson().toJson(parms));

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).editAdd4TimeAndMan(token, requestBody).enqueue(new Callback<AuthBaseBean<BindingBean>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<BindingBean>> call, Response<AuthBaseBean<BindingBean>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
//                    Toast.makeText(ControlActivity.this, "第" + groupId + "策略生效成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<BindingBean>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "第" + groupId + "策略生效失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //任意组 控灯亮度
    private void brightnessConbinationGroup(ArrayList<LampListBean> list) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            Gson gson = new Gson();
            String str = gson.toJson(list);

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    str);

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).brightnessConbinationGroup(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity.this, "任意组亮度调节成功", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "任意组亮度调节失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    //获取首页节能信息

    //控制灯光亮度
    private void getEnergyStatistics(String groupId, boolean isAll) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");
            Map<String, String> params = new HashMap<String, String>();
            if (isAll) {
                params.put("groupId", null);
            } else {
                params.put("groupId", groupId);
            }
            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    new JSONObject(params).toString());

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).getEnergyStatistics(token, requestBody).enqueue(new Callback<AuthBaseBean<MainEnergyBean>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<MainEnergyBean>> call, Response<AuthBaseBean<MainEnergyBean>> response) {
                    if (response.body() != null && response.body().getData() != null) {
                        tv_jrnh_num.setText(response.body().getData().getToday().getEnergy());
                        tv_jrnh_num2.setText(response.body().getData().getTodaySave().getSaveEnergy());
                        tv_jrnh_num3.setText(response.body().getData().getAllDaySave().getSaveEnergy());
                        tv_yxsj.setText("系统累计运行时长：" + response.body().getData().getEarliestInstallLampTime());
                    }
                }

                @Override
                public void onFailure(Call<AuthBaseBean<MainEnergyBean>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //任意组 控灯开关
    private void openOrCloseConbinationGroup(ArrayList<LampListBean> list, String message, boolean isOpen, boolean isAll) {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            Gson gson = new Gson();
            String str = gson.toJson(list);

            RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json;"),
                    str);

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).openOrClose(token, requestBody).enqueue(new Callback<AuthBaseBean<Object>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<Object>> call, Response<AuthBaseBean<Object>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    Toast.makeText(ControlActivity.this, message, Toast.LENGTH_SHORT).show();
                    if (isOpen) {
                        iv_light_open.setSelected(true);
                        setLightState(true);
                        if (isAll) {
                            isOpenGroupAll = true;
                        } else {
                            isOpenGroupRandom = true;
                        }
                    } else {
                        setLightState(false);
                        iv_light_open.setSelected(false);
                        if (isAll) {
                            isOpenGroupAll = false;
                        } else {
                            isOpenGroupRandom = false;
                        }
                    }
                    showLightState();
                    saveTime();
                }

                @Override
                public void onFailure(Call<AuthBaseBean<Object>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "操作失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //获取策略
    private BindingBean.Man getMan() {
        BindingBean.Man man = new BindingBean.Man();
        man.setDelayTime(Integer.valueOf(tv_cxsj_time.getText().toString()));
        man.setChangeNot(Integer.valueOf(tv_jbsj_time.getText().toString()));
        man.setMaxManLight(bassController.getAngel());
        man.setMinManLight(bassController2.getAngel());
        return man;
    }


    private BindingBean.Time getTime() {
        BindingBean.Time man = new BindingBean.Time();

        man.setOpenTime(tv_kdsj_time_hour.getText().toString() + ":" + tv_kdsj_time_min.getText().toString());
        man.setShutTime(tv_gdsj_time_hour.getText().toString() + ":" + tv_gdsj_time_min.getText().toString());
//        man.setLight(view_pg.getProgressNum());
        man.setJobType(1);
        return man;
    }

    private ArrayList<LampListBean> lampList = new ArrayList<>();
    LightAdapter lightAdapter ;
    //获取所有灯具信息
    private void getLampList() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).getLampList(token).enqueue(new Callback<LampInterfaceBean<ArrayList<LampListBean>>>() {
                @Override
                public void onResponse(Call<LampInterfaceBean<ArrayList<LampListBean>>> call, Response<LampInterfaceBean<ArrayList<LampListBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if (response != null && response.body() != null && response.body().getRows() != null) {
                        ArrayList<LampListBean> list = response.body().getRows();
                        refreashLight();
                        for (int i = 0; i < list.size(); i++) {
                            LampListBean bean = list.get(i);
                            bean.setSerialNum(i + 1 + "");
                            lampList.add(bean);
                        }
                        refreashLight();
                        setLamp();
                        for (int i = 0; i < lampList.size(); i++) {
                            int finalI = i;
                            checkBoxlist.get(i).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                    if (lampList.get(finalI).getLight() != null && !lampList.get(finalI).getLight().equals("")) {
                                        view_pg.setProgress(Integer.valueOf(lampList.get(finalI).getLight()));
                                    }
                                }
                            });
                        }
                    }
                    Toast.makeText(ControlActivity.this, "获取所有灯具信息成功", Toast.LENGTH_SHORT).show();


                }

                @Override
                public void onFailure(Call<LampInterfaceBean<ArrayList<LampListBean>>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "获取所有灯具信息失败" + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public void refreashLight() {
        for (int i = 0; i < lampList.size(); i++) {
            checkBoxlist.get(i).setVisibility(View.VISIBLE);
            imageViewList.get(i).setVisibility(View.VISIBLE);
        }
    }

    //区域选择提示
    private RegionBean nowRegion;

    private void getRegion() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(ControlActivity.this, url).create(AuthManageService.class).getRegion(token).enqueue(new Callback<AuthBaseBean<ArrayList<RegionBean>>>() {
                @Override
                public void onResponse(Call<AuthBaseBean<ArrayList<RegionBean>>> call, Response<AuthBaseBean<ArrayList<RegionBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if (response != null && response.body() != null && response.body().getData() != null && response.body().getData().size() != 0) {

                        if (nowRegion == null) {
                            nowRegion = response.body().getData().get(0);
                        }
                    } else {
                        Toast.makeText(ControlActivity.this, "获取区域信息失败", Toast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onFailure(Call<AuthBaseBean<ArrayList<RegionBean>>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());
                    Toast.makeText(ControlActivity.this, "获取区域信息失败", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //加载当前灯具位置信息
    public void setLamp() {
        for (int i = 0; i < lampList.size(); i++) {
            if (textViewList.size() > i) {
                String snId = lampList.get(i).getLampSn();
                textViewList.get(i).setText(snId);
            }
        }
    }

    Handler handler = new Handler();

    //根据亮灯状态 决定是否更新策略
    public void refreashWays() {
        if (System.currentTimeMillis() - lastWaysTime > 2000) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    /**
                     *要执行的操作
                     */
                    if (rl_group_all.isSelected()) {
                        if (isOpenGroupAll) {
                            editAdd4TimeAndMan(getGroupId(0));
                        }
                    } else if (rl_group1.isSelected()) {
                        if (isOpenGroup1) {
                            editAdd4TimeAndMan(getGroupId(0));
                        }
                    } else if (rl_group2.isSelected()) {
                        if (isOpenGroup2) {
                            editAdd4TimeAndMan(getGroupId(1));
                        }

                    } else if (rl_group3.isSelected()) {
                        if (isOpenGroup2) {
                            editAdd4TimeAndMan(getGroupId(2));
                        }
                    }
                }
            }, 2000);
        } else {

        }
    }
}